package com.lam.common.context;

public enum AuthTypeEnum {
    
	/** 后端认证 */
	ADMIN("adm", "adm"),
	/** 前端认证 */
	FRONT("front", "api");
	
	/** 认证类型 */
	private String authType;
	/** 登录用户Token前缀 */
	private String tokenPrefix;
	
	/** 登录用户Token令牌缓存KEY前缀 */
	private String tokenKey;
	/** 登录用户权限列表缓存KEY前缀 */
	private String permissionKey;
	/** 登录用户角色列表缓存KEY前缀 */
	private String roleKey;
	
	private AuthTypeEnum(String authType, String tokenPrefix) {
		this.authType = authType;
		this.tokenPrefix = tokenPrefix;
		
		this.tokenKey = String.format("loginUser:%s:%s:", authType, "user");
		this.permissionKey = String.format("loginUser:%s:%s:", authType, "permis");
		this.roleKey = String.format("loginUser:%s:%s:", authType, "role");
	}
	
	public static AuthTypeEnum valueFromAuthType(String authType) {
		for (AuthTypeEnum a : values()) {
			if(a.authType.equalsIgnoreCase(authType)) {
				return a;
			}
		}
		return null;
	}
	
	public static AuthTypeEnum valueFromTokenPrefix(String tokenPrefix) {
		for (AuthTypeEnum a : values()) {
			if(a.tokenPrefix.equalsIgnoreCase(tokenPrefix)) {
				return a;
			}
		}
		return null;
	}
	
	public static AuthTypeEnum valueFromToken(String token) {
		for (AuthTypeEnum a : values()) {
			if(token.startsWith(a.tokenPrefix)) {
				return a;
			}
		}
		return null;
	}
	
	public String getAuthType() {
		return authType;
	}
	public String getTokenPrefix() {
		return tokenPrefix;
	}
	public String getTokenKey() {
		return tokenKey;
	}
	public String getPermissionKey() {
		return permissionKey;
	}
	public String getRoleKey() {
		return roleKey;
	}
	
	
}
